<template>
  <div style="height:6vh;padding: 10px 20px;background-color: #fff;">
    <span style="line-height:60px;font-size: 20px;">用户管理</span>
    <el-button type="primary" style="float:right;margin-top:10px;"
               @click="beforeAddUser">新建用户</el-button>
  </div>
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="用户搜索" style="width: 220px;">
        <el-input placeholder="请输入用户名"
                  v-model="searchUserForm.username" @keydown.enter="loadUser"></el-input>
      </el-form-item>
      <el-form-item label="用户的角色" style="width: 290px;">
        <el-select placeholder="请选择用户角色"
                   v-model="searchUserForm.role" @change="loadUser">
          <el-option label="管理员" value="0"></el-option>
          <el-option label="求职者" value="1"></el-option>
          <el-option label="招聘者" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadUser">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 用户列表数据卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="userArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column label="公司编号" prop="companyId" align="center"></el-table-column>
      <el-table-column label="姓名" prop="username" align="center"></el-table-column>
      <el-table-column label="邮箱" prop="email" align="center"></el-table-column>
      <el-table-column label="最后一次登录时间" prop="lastLoginTime" align="center"></el-table-column>
      <el-table-column label="角色" prop="role" align="center"></el-table-column>
      <el-table-column label="电话" prop="phone" align="center"></el-table-column>
      <el-table-column label="用户状态" prop="userStatus" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0" v-model="scope.row.userStatus"
                     @change="changeStatus(scope.row.id,scope.row.userStatus)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="editUser(scope.row.id)">编辑</el-button>
          <el-button link type="primary" :disabled="scope.row.level==40"
                     @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- :before-close="handleClose"在关闭弹窗前调用处理弹窗关闭的方法,防止误触将弹窗关闭 -->
  <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      style="width: 1000px;padding: 40px;"
      :before-close="handleClose"
  >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名">
            <!-- :disabled="saveUserForm.id!=null"编辑员工时禁用用户名输入框,新增时没有用户id -->
            <el-input placeholder="请输入用户名" v-model="saveUserForm.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司编号">
            <el-input placeholder="请输入公司编号" v-model="saveUserForm.companyId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="密码">
            <el-input placeholder="请输入用户密码" v-model="saveUserForm.password"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话">
            <el-input placeholder="请输入电话" v-model="saveUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input placeholder="请输入邮箱" v-model="saveUserForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色">
            <el-select v-model="saveUserForm.role">
              <el-option label="管理员" value="0"></el-option>
              <el-option label="求职者" value="1"></el-option>
              <el-option label="招聘者" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-form-item label="性别">
          <el-radio-group v-model="saveUserForm.gender">
            <el-radio border label="男" :value=1 style="margin: 0;"></el-radio>
            <el-radio border label="女" :value=0 ></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveUser">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

const searchUserForm = ref({
  username: '',
  role: ''
})
//定义一个用户列表数组
const userArr = ref([]);
//定义一个新增用户的弹窗
const dialogTitle = ref('新增用户');
//定义一个显示弹窗
const dialogVisible = ref(false);
//定义一个保存用户的变量
const saveUserForm = ref({
  username: '',
  gender:'',
  phone: '',
  email: '',
  role: '',
  companyId: '',
  password: ''
})
//定义一个关闭弹窗之前的方法
const handleClose = () => {
  if(confirm('你确定要关闭弹窗吗')){
    dialogVisible.value = false;
  }
}
//定义一个搜索用户列表的方法
const loadUser = () => {
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASEURL+'/admin/query?'+data).then((response)=> {
    if (response.data.code === 2000) {
      userArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg)
    }
  })}
//定义一个新增用户的方法
const beforeAddUser = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新增用户';
}
//定义一个新增用户的方法
const saveUser = ()=>{
  let data = qs.stringify(saveUserForm.value);
  axios.post(BASEURL+'/admin/save',data).then((response)=>{
    if(response.data.code===2000){
      ElMessage.success('保存成功');
      saveUserForm.value={};
      dialogVisible.value=false;
      loadUser();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}


//定义一个重置用户的方法
const resetSearch = () => {
  searchUserForm.value={};
  loadUser();
}
//定义一个页面的刷新的方法
onMounted(()=>{
  loadUser();
})

//定义一个改变用户状态的方法
const changeStatus = (id,status)=>{
  axios.post(BASEURL+'/admin/update/status/'+id+'/'+status).then((response)=>{
    if(response.data.code===2000){
      ElMessage.success('修改用户状态成功');
      loadUser();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义一个编辑用户的方法
const editUser = (id)=>{
  dialogTitle.value="编辑用户给";
  dialogVisible.value=true;
  axios.get(BASEURL+'/admin/query?'+id).then((response)=>{
    if(response.data.code===2000){
      saveUserForm.value=response.data.data[0];
    } else {
      ELMessage.error(response.data.msg);
    }
  })
}
//定义一个删除用户的方法
const deleteUser = (id)=>{
  axios.post(BASEURL+'/admin/delete/'+id).then((response)=>{
    if(response.data.code===2000){
      ElMessage.success('删除用户成功');
      loadUser();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
</script>

<style scoped>


</style>